// 删除未使用的导入
// import { userApi } from '@/api'

// 模拟用户数据库
let mockUsers = [
    {
        id: 1,
        username: 'test',
        password: '123456', // 实际应用中密码应该加密存储
        nickname: '测试用户',
        email: 'test@example.com',
        avatar: 'https://via.placeholder.com/100'
    }
]

const state = {
    token: localStorage.getItem('user_token') || '',
    userInfo: null
}

const mutations = {
    SET_TOKEN(state, token) {
        state.token = token
        if (token) {
            localStorage.setItem('user_token', token)
        } else {
            localStorage.removeItem('user_token')
        }
    },
    SET_USER_INFO(state, userInfo) {
        state.userInfo = userInfo
    }
}

const actions = {
    async login({ commit }, loginForm) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                const user = mockUsers.find(u => 
                    u.username === loginForm.username && 
                    u.password === loginForm.password
                )
                
                if (user) {
                    const token = `mock-token-${user.id}-${Date.now()}`
                    const userInfo = { ...user }
                    delete userInfo.password // 不返回密码
                    
                    commit('SET_TOKEN', token)
                    commit('SET_USER_INFO', userInfo)
                    resolve({ token, userInfo })
                } else {
                    reject(new Error('用户名或密码错误'))
                }
            }, 300)
        })
    },

    logout({ commit }) {
        commit('SET_TOKEN', '')
        commit('SET_USER_INFO', null)
    },

    async getUserInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                const user = mockUsers.find(u => u.id === state.userInfo?.id)
                if (user) {
                    const userInfo = { ...user }
                    delete userInfo.password
                    commit('SET_USER_INFO', userInfo)
                    resolve(userInfo)
                } else {
                    reject(new Error('用户不存在'))
                }
            }, 100)
        })
    },

    async register({ commit }, registerData) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                // 检查用户名是否已存在
                const existingUser = mockUsers.find(user => user.username === registerData.username)
                if (existingUser) {
                    reject(new Error('用户名已存在'))
                    return
                }

                // 创建新用户
                const newUser = {
                    id: mockUsers.length + 1,
                    username: registerData.username,
                    password: registerData.password, // 实际应用中应该加密
                    email: registerData.email,
                    nickname: registerData.username,
                    avatar: 'https://via.placeholder.com/100'
                }

                // 添加到模拟数据库
                mockUsers.push(newUser)
                
                // 注册成功后自动登录
                const token = `mock-token-${newUser.id}-${Date.now()}`
                const userInfo = { ...newUser }
                delete userInfo.password
                
                commit('SET_TOKEN', token)
                commit('SET_USER_INFO', userInfo)
                resolve({ token, userInfo })
            }, 300)
        })
    },

    async updateProfile({ commit, state }, profileData) {
        return new Promise((resolve) => {
            setTimeout(() => {
                const userIndex = mockUsers.findIndex(u => u.id === state.userInfo.id)
                if (userIndex !== -1) {
                    mockUsers[userIndex] = {
                        ...mockUsers[userIndex],
                        ...profileData
                    }
                    const updatedUser = { ...mockUsers[userIndex] }
                    delete updatedUser.password
                    commit('SET_USER_INFO', updatedUser)
                    resolve(updatedUser)
                }
            }, 100)
        })
    },

    async getProductStatistics() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve({
                    totalProducts: 10,
                    activeProducts: 8,
                    soldProducts: 2
                })
            }, 100)
        })
    }
}

const getters = {
    isLoggedIn: state => !!state.token,
    userInfo: state => state.userInfo
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
} 